/****************************************************/
// Theme name: Nexus - Bootstrap Admin Template
// Theme URI: http://nexus.lethemes.net/1.0/html/
// Description: Nexus is a fully responsive admin dashboard template
// Tags: admin, admin dashboard, cms, crm, project managerment, responsive, webapp, backend, bootstrap admin, responsive admin
// Author: Loi Le
// Version: 1.0
/****************************************************/

/****************************************************/
// 01. Animo.js
// 02. Gauge.js
// 03. Malihu Scrollbar
// 04. Owl Carousel
// 05. Responsive Pagination
// 06. Summernote
// 07. Bootstrap DatePicker
// 08. Bootstrap Dual Listbox
// 09. Bootstrap Growl
// 10. Bootstrap Maxlength
// 11. Bootstrap Progressbar
// 12. Bootstrap Slider
// 13. Bootstrap SweetAlert
// 14. Bootstrap TouchSpin
// 15. jQuery Easy Pie Chart
// 16. jQuery MiniColors
// 17. jQuery Validation
// 18. jQuery Sparklines
// function Nexus() {}
// Initial 
/****************************************************/

'use strict';

// Animo.js
// --------------------------------------------------

function animoJS(element) {
    
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).animo({
                animation: $(this).attr('data-animation'),
                duration: $(this).attr('data-duration') || 0.3,
                iterate: $(this).attr('data-iterate') || 1,
                timing: $(this).attr('data-timing') || 'linear',
                keep: $(this).attr('data-keep') || false,
            })
        })
    }
}

// Gauge.js
// --------------------------------------------------
function gaugeJS(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            var target = document.getElementById($(this).attr('id'));
            var gauge = new Gauge(target).setOptions({
                lines: $(this).data('lines') || 12,
                angle: $(this).data('angle') || 0,
                lineWidth: $(this).data('lineWidth') || 0.4,
                pointer: $(this).data('pointer') || {
                    length: 0.9,
                    strokeWidth: 0.02,
                    color: '#264348'
                },
                limitMax: $(this).data('limitMax') || false,
                colorStart: $(this).data('colorStart') || '#6C70DA',
                colorStop: $(this).data('colorStop') || '#6C70DA',
                strokeColor: $(this).data('strokeColor') || '#DDD',
                generateGradient: $(this).data('generateGradient') || false
            });
            gauge.maxValue = $(this).data('maxValue');
            gauge.animationSpeed = $(this).data('animationSpeed ') || 32;
            gauge.set($(this).data('set'));
        })
    }
}

// Malihu Scrollbar
// --------------------------------------------------
function malihuScrollbar(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).mCustomScrollbar({
                theme: $(this).data('theme') || 'dark',
                autoHideScrollbar: $(this).data('autoHideScrollbar') || false,
                mouseWheel: {
                    scrollAmount: $(this).data('scrollAmount') || 'auto',
                },
            })
        })
    }
}

// Owl Carousel
// --------------------------------------------------
function owlCarousel(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).owlCarousel({
                loop: $(this).data('loop') || false,
                autoHeight: $(this).data('autoHeight') || false,
                autoWidth: $(this).data('autoWidth') || false,
                autoplay: $(this).data('autoplay') || false,
                autoplayTimeout: $(this).data('autoplayTimeout') || 3000,
                autoplayHoverPause: $(this).data('autoplayHoverPause') || false,
                margin: $(this).data('margin') || 10,
                stagePadding: $(this).data('stagePadding') || 0,
                rtl: $(this).data('rtl') || false,
                nav: $(this).data('nav') || false,
                lazyLoad: $(this).data('lazyLoad') || false,
                responsiveClass: $(this).data('responsiveClass') || false,
                center: $(this).data('center') || false,
                items: $(this).data('items') || 1,
                merge: $(this).data('merge') || false,
                video: $(this).data('video') || false,
                autoWidth: $(this).data('autoWidth') || false,
                animateIn: $(this).data('animateIn') || false,
                animateOut: $(this).data('animateOut') || false,
                smartSpeed: $(this).data('smartSpeed') || 300,
                responsive: {
                    0: {
                        items: $(this).data('extraItems') || $(this).data('items')
                    },
                    768: {
                        items: $(this).data('smallItems') || $(this).data('items')
                    },
                    992: {
                        items: $(this).data('mediumItems') || $(this).data('items')
                    },
                    1200: {
                        items: $(this).data('largeItems') || $(this).data('items')
                    }
                }
            })
        })
    }
}

// Responsive Pagination
// --------------------------------------------------
function responsivePagination(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).rPage();
        })
    }
}

// Summernote
// --------------------------------------------------
function summernote(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).summernote({
                height: $(this).data('height') || 500,
                toolbar: $(this).data('toolbar') || [
                    ['style', ['style']],
                    ['font', ['bold', 'italic', 'underline', 'clear']],
                    ['fontname', ['fontname']],
                    ['fontsize', ['fontsize']],
                    ['color', ['color']],
                    ['para', ['ul', 'ol', 'paragraph']],
                    ['height', ['height']],
                    ['table', ['table']],
                    ['insert', ['link', 'picture', 'hr']],
                    ['view', ['fullscreen']],
                ],
                airMode: $(this).data('airMode') || false,
            })
        })
    }
}

// Bootstrap DatePicker
// --------------------------------------------------
function bootstrapDatePicker(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).datetimepicker({
                format: $(this).data('format') || false,
                locale: $(this).data('locale') || 'en-gb',
                defaultDate: $(this).data('defaultDate') || false,
                disabledDates: $(this).data('disabledDates') || false,
                enabledDates: $(this).data('enabledDates') || false,
                icons: {
                    time: $(this).data('timeIcon') || 'zmdi zmdi-time',
                    date: $(this).data('dateIcon') || 'zmdi zmdi-calendar',
                    up: $(this).data('upIcon') || 'zmdi zmdi-chevron-up',
                    down: $(this).data('downIcon') || 'zmdi zmdi-chevron-down',
                    previous: $(this).data('previousIcon') || 'zmdi zmdi-chevron-left',
                    next: $(this).data('nextIcon') || 'zmdi zmdi-chevron-right',
                    today: $(this).data('todayIcon') || 'zmdi zmdi-gps-dot',
                    clear: $(this).data('clearIcon') || 'zmdi zmdi-delete',
                    close: $(this).data('closeIcon') || 'zmdi zmdi-close',
                },
                viewMode: $(this).data('viewMode') || 'days',
                daysOfWeekDisabled: $(this).data('daysOfWeekDisabled') || [],
                inline: $(this).data('inline') || false,
                sideBySide: $(this).data('sideBySide') || false,
            })
        })
    }
}

// Bootstrap Dual Listbox
// --------------------------------------------------
function bootstrapDualListbox(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).bootstrapDualListbox({
                filterTextClear: $(this).data('filterTextClear') || 'show all',
                filterPlaceHolder: $(this).data('filterPlaceHolder') || 'Filter',
                moveSelectedLabel: $(this).data('moveSelectedLabel') || 'Move selected',
                moveAllLabel: $(this).data('moveAllLabel') || 'Move all',
                removeSelectedLabel: $(this).data('removeSelectedLabel') || 'Remove selected',
                removeAllLabel: $(this).data('removeAllLabel') || 'Remove all',
                moveOnSelect: $(this).data('moveOnSelect') || false,
                preserveSelectionOnMove: $(this).data('preserveSelectionOnMove') || false,
                selectedListLabel: $(this).data('selectedListLabel') || false,
                nonSelectedListLabel: $(this).data('nonSelectedListLabel') || false,
                helperSelectNamePostfix: $(this).data('helperSelectNamePostfix') || '_helper',
                selectorMinimalHeight: $(this).data('selectorMinimalHeight') || 300,
                showFilterInputs: $(this).data('showFilterInputs') || false,
                nonSelectedFilter: $(this).data('nonSelectedFilter') || '',
                selectedFilter: $(this).data('selectedFilter') || '',
                infoText: $(this).data('infoText') || 'Showing all {0}',
                infoTextFiltered: $(this).data('infoTextFiltered') || '<span class="label label-warning">Filtered</span> {0} from {1}',
                infoTextEmpty: $(this).data('infoTextEmpty') || 'Empty list',
                filterOnValues: $(this).data('filterOnValues') || false,
            })
        })
    }
}

// Bootstrap Growl
// --------------------------------------------------
function bootstrapGrowl(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $.bootstrapGrowl($(this).data('message'), {
                ele: $(this).data('ele') || 'body',
                type: $(this).data('type') || 'info',
                offset: {
                    from: $(this).data('from') || 'top',
                    amount: $(this).data('amount') || 20
                },
                align: $(this).data('align') || 'right',
                width: $(this).data('width') || 300,
                delay: $(this).data('delay') || 3000,
                allow_dismiss: $(this).data('allowDismiss') || false,
                stackup_spacing: $(this).data('stackupSpacing') || 10
            })
        })
    }
}

// Bootstrap Maxlength
// --------------------------------------------------
function bootstrapMaxlength(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).maxlength({
                alwaysShow: $(this).data('alwaysShow') || false,
                threshold: $(this).data('threshold') || 10,
                warningClass: $(this).data('warningClass') || 'label label-success',
                limitReachedClass: $(this).data('limitReachedClass') || 'label label-danger',
                separator: $(this).data('separator') || '/',
                preText: $(this).data('preText') || '',
                postText: $(this).data('postText') || '',
                showMaxLength: $(this).data('showMaxLength') || false,
                showCharsTyped: $(this).data('showCharsTyped') || false,
                placement: $(this).data('placement') || 'centered-right',
            })
        })
    }
}

// Bootstrap Progressbar
// --------------------------------------------------
function bootstrapProgressbar(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).progressbar({
                display_text: $(this).data('displayText') || 'none',
                use_percentage: $(this).data('usePercentage') || false,
                transition_delay: $(this).data('transitionDelay') || 0,
                refresh_speed: $(this).data('refreshSpeed') || 0,
                percent_format: function(percent) {
                    return percent + '%';
                },
                amount_format: function(amount_part, amount_total) {
                    return amount_part + ' / ' + amount_total;
                }
            })
        })
    }
}

// Bootstrap Slider
// --------------------------------------------------
function bootstrapSlider(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            var formatter = $(this).data('sliderFormatter') || '';
            $(this).slider({
                reversed: $(this).data('sliderReversed') || false,
                formatter: function(value) {
                    return formatter + value
                },
            })
        })
    }
}

// Bootstrap SweetAlert
// --------------------------------------------------
function bootstrapSweetAlert(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            swal({
                title: $(this).data('title') || 'Here\'s a message!',
                text: $(this).data('text') || null,
                type: $(this).data('type') || null,
                timer: $(this).data('timer') || null,
                imageUrl: $(this).data('imageUrl') || null,
                showCancelButton: $(this).data('showCancelButton') || false,
                confirmButtonClass: $(this).data('confirmButtonClass') || 'btn-primary',
                confirmButtonText: $(this).data('confirmButtonText') || 'OK',
                cancelButtonClass: $(this).data('cancelButtonClass') || 'btn-outline btn-black',
                cancelButtonText: $(this).data('cancelButtonText') || 'Cancel',
                closeOnConfirm: $(this).data('closeOnConfirm') || false,
                closeOnCancel: $(this).data('closeOnCancel') || false,
            })
        })
    }
}

// Bootstrap TouchSpin
// --------------------------------------------------
function bootstrapTouchspin(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).TouchSpin({
                initval: $(this).data('initval') || '',
                min: $(this).data('min') || 0,
                max: $(this).data('max') || 100,
                step: $(this).data('step') || 1,
                forcestepdivisibility: $(this).data('forceStepDivisibility') || 'round',
                decimals: $(this).data('decimals') || 0,
                stepinterval: $(this).data('stepInterval') || 100,
                stepintervaldelay: $(this).data('stepIntervalDelay') || 500,
                verticalbuttons: $(this).data('verticalButtons') || false,
                verticalupclass: $(this).data('verticalUpClass') || 'glyphicon glyphicon-chevron-up',
                verticaldownclass: $(this).data('verticalDownClass') || 'glyphicon glyphicon-chevron-down',
                prefix: $(this).data('prefix') || '',
                postfix: $(this).data('postfix') || '',
                prefix_extraclass: $(this).data('prefixExtraClass') || '',
                postfix_extraclass: $(this).data('postfixExtraClass') || '',
                booster: $(this).data('booster') || false,
                boostat: $(this).data('boostat') || 10,
                maxboostedstep: $(this).data('maxBoostedStep') || false,
                mousewheel: $(this).data('mouseWheel') || false,
                buttondown_class: $(this).data('buttonDownClass') || 'btn btn-outline btn-default',
                buttonup_class: $(this).data('buttonUpClass') || 'btn btn-outline btn-default',
            })
        })
    }
}

// jQuery Easy Pie Chart
// --------------------------------------------------
function jQueryEasyPieChart(element) {
    //if ($(element).length > 0) {
    //    $(element).easyPieChart({
    //        barColor: '#3B40CE',
    //        trackColor: '#22A7F0',
    //        scaleColor: false,
    //        scaleLength: 0,
    //        lineCap: 'butt',
    //        lineWidth: 5,
    //        size: 120,
    //        rotate: 0,
    //        animate: {
    //            duration: 1000,
    //            enabled: true
    //        },
    //        easing: 'easeOutBounce',
    //        onStep: function(from, to, percent) {
    //            $(this.el).find('.percent').text(Math.round(percent));
    //        },
    //    })
    //}
}

// jQuery MiniColors
// --------------------------------------------------
function jQueryMiniColors(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).minicolors({
                control: $(this).data('control') || 'hue',
                defaultValue: $(this).data('defaultValue') || '',
                format: $(this).data('format') || 'hex',
                inline: $(this).data('inline') || false,
                keywords: $(this).data('keywords') || '',
                letterCase: $(this).data('letterCase') || 'lowercase',
                opacity: $(this).data('opacity') || false,
                position: $(this).data('position') || 'bottom left',
                theme: 'bootstrap',
                change: function(hex, opacity) {
                    console.log(hex + ' - ' + opacity);
                },
                show: function() {
                    if ($(this).data('animateEnabled')) {
                        $(this).nextAll('.minicolors-panel').animo({
                            animation: $(this).data('animation') || 'fadeInUp',
                            duration: $(this).data('duration') || .3
                        });
                    }
                },
            })
        })
    }
}

// jQuery Validation
// --------------------------------------------------
function jQueryValidation(element) {
    if ($(element).length > 0) {
        $(element).each(function() {
            $(this).validate({
                highlight: function(element) {
                    $(element).closest('.form-group').addClass('has-error');
                },
                unhighlight: function(element) {
                    $(element).closest('.form-group').removeClass('has-error');
                },
                errorElement: 'span',
                errorClass: 'help-block',
                errorPlacement: function(error, element) {
                    if (element.parent('.input-group').length) {
                        error.insertAfter(element.parent());
                    } else if (element.parent('label').length) {
                        error.insertBefore(element.parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            })
        })
    }
}

// jQuery Sparklines
// --------------------------------------------------
function sparklineBar(element, values, height, barWidth, barSpacing, barColor, negBarColor) {
    $(element).sparkline(values, {
        type: 'bar',
        height: height,
        barWidth: barWidth,
        barSpacing: barSpacing,
        barColor: barColor,
        negBarColor: negBarColor
    })
}

function sparklineLine(element, values, width, height, lineColor, fillColor, lineWidth, spotColor, minSpotColor, maxSpotColor, highlightSpotColor, highlightLineColor, spotRadius) {
    $(element).sparkline(values, {
        type: 'line',
        width: width,
        height: height,
        lineColor: lineColor,
        fillColor: fillColor,
        lineWidth: lineWidth,
        spotColor: spotColor,
        minSpotColor: minSpotColor,
        maxSpotColor: maxSpotColor,
        highlightSpotColor: highlightSpotColor,
        highlightLineColor: highlightLineColor,
        spotRadius: spotRadius
    });
}

function sparklinePie(element, values, width, height, sliceColors) {
    $(element).sparkline(values, {
        type: 'pie',
        width: width,
        height: height,
        sliceColors: sliceColors,
    });
}

function Nexus() {
    this.initAnimoJS = animoJS('.animated');
    this.initGaugeJS = gaugeJS('.gauge-js');
    this.initMalihuScrollbar = malihuScrollbar('.malihu-scrollbar');
    this.initOwlCarousel = owlCarousel('.owl-carousel');
    this.initResponsivePagination = responsivePagination($('.pagination').filter('.responsive'));
    this.initSummernote = summernote('.summernote');
    this.initBootstrapDatePicker = bootstrapDatePicker('.date-picker');
    this.initBootstrapDualListbox = bootstrapDualListbox('.dual-listbox');
    this.initBootstrapGrowl = bootstrapGrowl('.growl');
    this.initBootstrapMaxlength = bootstrapMaxlength('.max-length');
    this.initBootstrapProgressbar = bootstrapProgressbar($('.progress').find('.progress-bar'));
    this.initBootstrapSlider = bootstrapSlider('.slider');
    this.initBootstrapSweetAlert = bootstrapSweetAlert('.sweet-alert');
    this.initBootstrapTouchspin = bootstrapTouchspin('.touchspin');
    this.initjQueryEasyPieChart = jQueryEasyPieChart('.easy-pie-chart');
    this.initjQueryMiniColors = jQueryMiniColors('.mini-colors');
    this.initjQueryValidation = jQueryValidation('.validation');
}

$(document).ready(function() {
    var nexus = new Nexus();
    nexus.initAnimoJS;
    nexus.initGaugeJS;
    nexus.initMalihuScrollbar;
    nexus.initOwlCarousel;
    nexus.initResponsivePagination;
    nexus.initSummernote;
    nexus.initBootstrapDatePicker;
    nexus.initBootstrapDualListbox;
    nexus.initBootstrapGrowl;
    nexus.initBootstrapMaxlength;
    nexus.initBootstrapProgressbar;
    nexus.initBootstrapSlider;
    nexus.initBootstrapSweetAlert;
    nexus.initBootstrapTouchspin;
    nexus.initjQueryEasyPieChart;
    nexus.initjQueryMiniColors;
    nexus.initjQueryValidation;
})